<template>

  <div class="container">
    <div class="app-container">

      <el-button class="btn-add" size="mini" type="primary" @click="addPermission(0, 1)">添加权限</el-button>

      <!--
        table 支持树形结构
         1. row-key="id" 表示当前行的唯一标识
         2. default-expand-all 展开所有
      -->
      <el-table :data="permissionList" row-key="id" default-expand-all>
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template #default="{ row}">
            <!-- 一级权限 type === 1 才能添加子权限 -->
            <!-- 二级权限 type === 2 控制页面中的按钮是否显示 / 禁用 -->
            <el-button v-if="row.type === 1" size="mini" type="text" @click="addPermission(row.id, 2)">添加</el-button>
            <el-button size="mini" type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 放置一个弹层 用来编辑新增节点 -->
    <el-dialog :title="`${showText}`" :visible="showDialog" @close="cancel">
      <!-- 表单 -->
      <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" style="width:90%" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="confirm">确定</el-button>
          <el-button size="small" @click="cancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { getPermissionList, addPermission, deletePermission, getPermissionDetail, updatePermission } from '@/api/permission'
import { translateListToTree } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      permissionList: [], // 权限列表
      formData: {
        name: '', // 权限名称
        code: '', // 权限标识
        description: '', // 权限描述
        type: null, // 权限类型
        pid: null, // 父级权限id
        enVisible: null // 权限点开启状态
      },
      showDialog: false, // 控制弹框的显示与隐藏
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }

    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑权限点' : '添加权限点'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取权限列表
    async getPermissionList() {
      const res = await getPermissionList()
      this.permissionList = translateListToTree(res, 0)
      // console.log(res)
    },
    // 添加权限/添加子权限
    async addPermission(pid, type) {
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
    },
    // 取消
    cancel() {
      this.showDialog = false
      this.$refs.perForm.resetFields()
      this.formData = {}
    },
    // 确定
    async confirm() {
      console.log('确定')
      // 1. 校验整个表单
      await this.$refs.perForm.validate()
      if (this.formData.id) {
        await updatePermission(this.formData)
        this.$message.success('修改权限成功')
      } else {
        // 2. 发送请求
        await addPermission(this.formData)
        // 3· 提示用户
        this.$message.success('添加权限成功')
      }
      // 4. 关闭弹层 + 重置表单
      this.cancel()

      // 5. 刷新权限列表
      this.getPermissionList()
    },
    // 删除权限点
    async delPermission(id) {
      try {
        // 1. 提示用户是否确定删除
        await this.$confirm('此操作将永久删除该文件, 是否继续?')
        // 2. 发送请求
        await deletePermission(id)
        // 3. 提示用户删除成功
        this.$message.success('删除成功')
        // 4. 重新获取权限列表
        this.getPermissionList()
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑权限点
    async editPermission(id) {
      console.log(id, this.formData.pid)
      // 1. 显示弹层
      this.showDialog = true
      // 2. 获取列表详情
      const res = await getPermissionDetail(id)
      // console.log(res)
      // 3. 将数据回填到表单中
      this.formData = res
    }
  }
}
</script>
<style>
.btn-add {
  margin: 10px;
}
</style>
